package chapter3.lc345;

//LC 345 反转字符串中的元音字母

/**
 * 双指针夹逼swap，遇到非元音就跳过
 */
public class ReverseVowelsOfString {
    private static final String vowels = "aeiouAEIOU";
    public String reverseVowels(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0, j = chars.length -1; i < j; ) {
            if (!isVowels(chars[i])) {
                i++;
                continue;
            }
            if (!isVowels(chars[j])) {
                j--;
                continue;
            }
            swap(chars, i++, j--);
        }
        return new String(chars);
    }

    private void swap(char[] s, int i, int j) {
        char temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }

    private boolean isVowels(char c) {
        return vowels.indexOf(c) != -1;
    }
}
